// import {
//   WSController,
//   OnWSMessage,
//   Provide,
//   OnWSConnection,
//   Inject,
//   WSEmit,
//   OnWSDisConnection,
// } from '@midwayjs/decorator';

// import { CustomSocketIOContext, SocketRequestEvent, SocketResponseEvent } from '../interface';

// import { ChildProcess } from 'child_process';
// import { UserService } from '../service/user';

// import { InjectEntityModel } from '@midwayjs/orm';
// import { Repository } from 'typeorm';
// import { Users } from '../entity/Users';
// import * as QueryString from 'querystring'
// import * as URL from 'url'
// import * as jwt from 'jsonwebtoken';
// import { Collectors } from '../entity/Collectors';

// @Provide()
// @WSController('/')
// export class HelloSocketController {
//   @Inject()
//   ctx: CustomSocketIOContext;

//   @Inject()
//   userService: UserService;

//   @InjectEntityModel(Users)
//   userModel: Repository<Users>;

//   childs: Map<string, ChildProcess>;

//   handle: any;

//   constructor() {
//     this.childs = new Map()
//   }

//   @OnWSConnection()
//   async onConnectionMethod() {
//     console.log('on client connect', this.ctx.id);

//     // this.handle = setInterval(()=>{
//     //   this.ctx.emit('Hello',"let's play a game HAHA2 (too)")
//     // },2000)

//     let uri = URL.parse(this.ctx.request.url)
//     let querys = QueryString.parse(uri.query)

//     let secretKey = this.ctx.app.getConfig('token.secretKey')
//     let userInfo = null
//     try {
//       userInfo = jwt.verify(querys.token, secretKey);
//     } catch (err) {
//       //ctx.error(ctx.app.ErrorCode.AUTH_INVALID);
//       this.ctx.disconnect(true)
//       return;
//     }

//     let collectors = await this.userModel.query(`SELECT
//     u.id as user_id,
//     u.name as user_name,
//     p.id as company_id,
//     p.name as company_name,
//     c.serise_id as serise_id,
//     c.name as collecor_name,
//     c.push_host as push_host,
//     c.push_port as push_port,
//     c.uri as push_url
//     FROM
//     (SELECT id,name,company_id FROM users WHERE id=${userInfo.user_id}) as u
//     LEFT JOIN (SELECT id,name FROM companys) as p ON p.id=u.company_id
//     LEFT JOIN (SELECT company_id,serise_id,name,push_host,push_port,uri FROM collectors) as c  ON c.company_id=u.company_id`)
//     //   console.log(userInfo)

//     //   let now = Math.floor(Date.now() / 1000)
//     //   if (!!userInfo.expireTime && userInfo.expireTime < now) {
//     //     ctx.error(ctx.app.ErrorCode.AUTH_EXPIRED);
//     //     return;
//     //   }

//     if(collectors == 0)
//     {
//       this.ctx.disconnect(true)
//       return
//     }

//     let collector:Collectors = collectors[0]

//     let port = collector.push_port

//     this.ctx.rid = port

//     this.userService.JoinRoom(port, this.ctx.id, this.ctx)
//   }

//   @OnWSDisConnection()
//   async Disconnection() {
//     console.log('on client disconnect', this.ctx.id);

//     let rid = this.ctx.rid

//     this.userService.LeaveRoom(rid, this.ctx.id, this.ctx)
//   }

//   @OnWSMessage(SocketRequestEvent.GREET)
//   @WSEmit(SocketResponseEvent.GREET)
//   async gotMessage(data1, data2, data3) {
//     return {
//       name: 'harry',
//       result: data1 + data2 + data3,
//     };
//   }

//   @OnWSMessage('send')
//   @WSEmit('send')
//   async gotSend(data1) {
//     //console.log(data1)

//     let ctx: any = this.ctx
//     let rid = ctx.rid

//     this.userService.SendToProcess(rid, data1)

//     return {
//       name: 'harry',
//       result: data1,
//     };
//   }

// }
